package com.exmaple.lemlin.mvp.presenter;

import android.content.Context;

import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.entity.FriendGood;
import com.exmaple.lemlin.entity.FriendGoodDao;
import com.exmaple.lemlin.mvp.base.BasePresenter;
import com.exmaple.lemlin.mvp.view.IMainView;
import com.exmaple.lemlin.xmpp.XmppConnection;

import org.greenrobot.greendao.query.QueryBuilder;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.roster.packet.RosterPacket;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;

public class MainPresenter extends BasePresenter<IMainView> {

    public MainPresenter(Context context, IMainView view) {
        super(context, view);
    }

    public Disposable allFriendGoods() {

        Observable<FriendGood> observable = Observable.just(XmppConnection.getInstance().getAllEntries())
                .subscribeOn(Schedulers.io())
                .filter(new Predicate<List<RosterEntry>>() {
                    @Override
                    public boolean test(List<RosterEntry> entries) throws Exception {
                        if (entries.size() == 0) {
                            APPIM.getDaoSession().getFriendGoodDao().deleteAll();
                        }
                        return entries.size() != 0;
                    }
                })
                .flatMap(new Function<List<RosterEntry>, ObservableSource<RosterEntry>>() {
                    @Override
                    public ObservableSource<RosterEntry> apply(List<RosterEntry> entries) throws Exception {
                        return Observable.fromIterable(entries);
                    }
                })
                .filter(new Predicate<RosterEntry>() {
                    @Override
                    public boolean test(RosterEntry rosterEntry) throws Exception {
                        QueryBuilder<FriendGood> builder = APPIM.getDaoSession().getFriendGoodDao().queryBuilder()
                                .where(FriendGoodDao.Properties.Name.eq(rosterEntry.getName()));
                        return builder.unique() == null && rosterEntry.getType() == RosterPacket.ItemType.both;
                    }
                })
                .map(new Function<RosterEntry, FriendGood>() {
                    @Override
                    public FriendGood apply(RosterEntry rosterEntry) throws Exception {
                        FriendGood friend = new FriendGood();
                        friend.setName(rosterEntry.getName());
                        friend.setSub(rosterEntry.getType().toString());
                        friend.setJid(rosterEntry.getJid().toString());
                        return friend;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread());
        return observable.subscribe(new Consumer<FriendGood>() {
            @Override
            public void accept(FriendGood friendGood) throws Exception {
                APPIM.getDaoSession().getFriendGoodDao().insert(friendGood);
                mView.onFriendGood(friendGood);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onHint("获取用户列表失败：" + throwable.getMessage());
            }
        });
    }

    public boolean isAuthenticated() {
        return XmppConnection.getInstance().isAuthenticated();
    }
}
